<?php

/**
 *    合作伙伴控制器
 *
 *    @author    Garbin
 *    @usage    none
 */
class CourseorderApp extends BackendApp
{
    var $_courseorder_mod;

    function CourseorderApp()
    {
        parent::BackendApp();

        $this->_courseorder_mod =& m('courseorder');
    }
    /**
     *    管理
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function index()
    {
        $search_options = array(
            'cc.name'   => Lang::get('course_name'),
            'order_sn'   => Lang::get('order_sn'),
            'cm.user_name'   => Lang::get('user_name'),
            'order_sn'   => Lang::get('order_sn'),
        );
        /* 默认搜索的字段是店铺名 */
        $field = 'course_name';
        array_key_exists($_GET['field'], $search_options) && $field = $_GET['field'];
        $conditions = $this->_get_query_conditions(array(array(
            'field' => $field,       //按用户名,店铺名,支付方式名称进行搜索
            'equal' => 'LIKE',
            'name'  => 'search_name',
        ),array(
            'field' => 'status',
            'equal' => '=',
            'type'  => 'numeric',
        ),array(
            'field' => 'co.add_time',
            'name'  => 'add_time_from',
            'equal' => '>=',
            'handler'=> 'gmstr2time',
        ),array(
            'field' => 'co.add_time',
            'name'  => 'add_time_to',
            'equal' => '<=',
            'handler'   => 'gmstr2time_end',
        ),array(
            'field' => 'co.order_amount',
            'name'  => 'order_amount_from',
            'equal' => '>=',
            'type'  => 'numeric',
        ),array(
            'field' => 'co.order_amount',
            'name'  => 'order_amount_to',
            'equal' => '<=',
            'type'  => 'numeric',
        ),
        ));

        $page   =   $this->_get_page(10);    //获取分页信息
        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort  = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order,array('asc','desc')))
            {
                $sort  = 'add_time';
                $order = 'desc';
            }
        }
        else
        {
            $sort  = 'add_time';
            $order = 'desc';
        }
        $sql_count = 'SELECT COUNT(1) FROM cw_courseorder AS co
                LEFT JOIN cw_course AS cc ON cc.course_id = co.course_id
                LEFT JOIN cw_member AS cm ON cm.user_id = co.buyer_id
                WHERE 1=1 '.$conditions.' ORDER BY co.add_time DESC';
        $res_count = $this->_courseorder_mod->getOne($sql_count);

        $sql = 'SELECT co.*,cc.name,cm.user_name,cm.real_name FROM cw_courseorder AS co
                LEFT JOIN cw_course AS cc ON cc.course_id = co.course_id
                LEFT JOIN cw_member AS cm ON cm.user_id = co.buyer_id
                WHERE 1=1 '.$conditions.' ORDER BY add_time DESC LIMIT '.$page['limit'];
        $courseorders = $this->_courseorder_mod->getAll($sql);

        $page['item_count'] = $res_count;   //获取统计的数据
        $this->_format_page($page);
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        /*
        $this->assign('order_status_list', array(
            '已支付' => '已支付',
            '未付款' => '未付款',
            '已取消' => '已取消',
            ORDER_PENDING => Lang::get('order_pending'),
            ORDER_SUBMITTED => Lang::get('order_submitted'),
            ORDER_SHIPPED => Lang::get('order_shipped'),
            ORDER_FINISHED => Lang::get('order_finished'),
            ORDER_CANCELED => Lang::get('order_canceled'),
        ));
        */
        $this->assign('search_options', $search_options);
        $this->assign('page_info', $page);          //将分页信息传递给视图，用于形成分页条
        $this->assign('courseorders', $courseorders);
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->display('courseorder.index.html');
    }

    /**
     *    查看
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function view()
    {
        $order_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$order_id)
        {
            $this->show_warning('no_such_order');
            return;
        }
        $sql = 'SELECT co.*,cc.name,cm.user_name,cm.real_name,cc.pic,cc.price,cc.title,cc.lessons,cc.fit FROM cw_courseorder AS co
                LEFT JOIN cw_course AS cc ON cc.course_id = co.course_id
                LEFT JOIN cw_member AS cm ON cm.user_id = co.buyer_id
                WHERE order_id = '.$order_id;
        $courseorders = $this->_courseorder_mod->getRow($sql);

        if (!$courseorders)
        {
            $this->show_warning('no_such_order');
            return;
        }
        $this->assign('courseorder', $courseorders);
        //$this->assign($order_detail['data']);
        $this->display('courseorder.view.html');
    }
    function cancel(){
    	$order_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$order_id)
        {
            $this->show_warning('no_such_order');
            return;
        }
    	$data = array(
    		'status' =>'已取消',
    	);

        if (!$this->_courseorder_mod->edit($order_id,$data))
        {
            $this->show_warning('操作失败！');
            return;
        }
        $this->show_message('操作成功',
                'back_list',    'index.php?app=courseorder',
                '返回详情', 'index.php?app=courseorder&amp;act=view&amp;id=' . $order_id
            );
    }
}
